package com.propertyportal.manageagent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.propertyportal.manageproperty.R;

public class AddPerson extends SherlockFragment{
	private EditText fname;
	private EditText lname;
	private EditText birthday;
	private EditText mobile;
	private EditText landline;
	private EditText email;
	private Button add;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View view = inflater.inflate(R.layout.addperson, container, false);
		fname=(EditText)view.findViewById(R.id.firstname);
		lname=(EditText)view.findViewById(R.id.lastname);
		birthday=(EditText)view.findViewById(R.id.birthday);
		mobile=(EditText)view.findViewById(R.id.mobile);
		landline=(EditText)view.findViewById(R.id.landline);
		email=(EditText)view.findViewById(R.id.email);
		add=(Button)view.findViewById(R.id.addPerson);
		
		add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//Toast.makeText(getActivity(), "CLICK", Toast.LENGTH_SHORT).show();
				//addPerson();
				postData();
				//doGet("http://192.168.2.38:8080/STTHERESEREALTYWS/webresources/com.propertyportal.entities.person/findall");
				//callWebService("http://192.168.2.38:8080/WebApplication7/webresources/com.propertyportal.entities.person/");
			}
		});
		return view;
	}
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}
	public void postData() {
		
		String urla = "http://192.168.2.38:8080/STTHERESEREALTYWS/webresources/com.propertyportal.entities.person/createPerson/";
        try {
            URL url = new URL(urla + URLEncoder.encode("", "ISO-8859-1") + "/" + URLEncoder.encode("", "ISO-8859-1") + "/" + "2013-10-2" + "/" + "1231" + "/" + "12345" + "/" + "@botbot");
            //  URL url = new URL(urla+from+"/"+to);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Content-Type", "application/json");
            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            } else {
                System.out.print("Succes");
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
		 }
	private void doGet(String url){
		try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            request.addHeader("Content-Type", "application/json");
            
            HttpResponse response = httpclient.execute(request);
            
            HttpEntity entity = response.getEntity();
            InputStream instream = entity.getContent();
            String jaxrsmessage = read(instream);
            System.out.println(jaxrsmessage);
       } catch (ClientProtocolException e) {
            e.printStackTrace();
       } catch (IOException e) {
            e.printStackTrace();
       }
    }
	private static String read(InputStream instream) {
        StringBuilder sb = null;
        try {
             sb = new StringBuilder();
             BufferedReader r = new BufferedReader(new InputStreamReader(
                       instream));
        for (String line = r.readLine(); line != null; line = r.readLine()) {
             	sb.append(line);
			}

			instream.close();

        } catch (IOException e) {
        }
        return sb.toString();

   }
	
}
